using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using GarbageTruck;

namespace Hipocampo.Recurrence.TestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        System.Collections.SortedList DateUnits = new System.Collections.SortedList();
        System.Collections.SortedList Amounts = new System.Collections.SortedList();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.DOTMDayComboBox.DataSource = Enum.GetValues(typeof(DayOfWeek));
            this.WOTYMonthComboBox.DataSource = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;

            DateUnits.Add("d", "Day");
            DateUnits.Add("w", "Week");
            DateUnits.Add("m", "Month");
            DateUnits.Add("y", "Year");
            this.DateUnitUnitcomboBox.DataSource = DateUnits.Values;

            Amounts.Add(1, "Minute");
            Amounts.Add(2, "Hour");
            Amounts.Add(3, "Day");
            Amounts.Add(4, "Week");
            this.DateUnitUnitAmountComboBox.DataSource = Amounts.Values;
                   
            
        }

        private TimeSpan GetTimeSpamFromAmount(int amountIndex) 
        {
            TimeSpan returnValue = new TimeSpan();

            switch (amountIndex) 
            {
                case 0:
                    returnValue = TimeSpan.FromMinutes(1);
                    break;

                case 1:
                    returnValue = TimeSpan.FromHours(1);
                    break;

                case 2:
                    returnValue = TimeSpan.FromDays(1) + TimeSpan.FromHours(3);
                    break;

                case 3:
                    returnValue = TimeSpan.FromDays(7);
                    break;

            }

            return returnValue;
        }

        private void DOTMDayCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            this.DOTMDayComboBox.Enabled = this.DOTMDayCheckBox.Checked;
        }

        private void DOTMShowButton_Click(object sender, EventArgs e)
        {
            frmTemporalExpressionTest testForm = new frmTemporalExpressionTest();
            ITemporalExpression temporalExpression = 
                TemporalExpressionFactory.CreateFromString("DOTM(" + 
                this.DOTMNumberNumericUpDown.Value.ToString() + 
                (this.DOTMDayCheckBox.Checked? "," + ((int)((DayOfWeek)this.DOTMDayComboBox.SelectedItem)).ToString() : "") + ")");
            testForm.TemporalExpression = temporalExpression;
            testForm.Title = "Day of the month: " + temporalExpression.ToString();

            string[] dayNames = Enum.GetNames(typeof(DayOfWeek));
            string dayNamesList = "";
            for (int i = 0; i < dayNames.Length; i++)
            {
                dayNamesList += i.ToString() + "-" + dayNames[i] + " ";
            }
            testForm.Description = "DOTM(n<,d>) Every n-number < d-day of the week* > of every month.  A negative value of n means reverse counting.\n" +
                "*: " + dayNamesList;
            testForm.ShowDialog();

        }

        private void WOTYMonthCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            this.WOTYMonthComboBox.Enabled = this.WOTYMonthCheckBox.Checked;

        }

        private void WOTYShowButton_Click(object sender, EventArgs e)
        {
            frmTemporalExpressionTest testForm = new frmTemporalExpressionTest();
            ITemporalExpression temporalExpression =
                TemporalExpressionFactory.CreateFromString("WOTY(" +
                this.WOTYNumberNumericUpDown.Value.ToString() +
                (this.WOTYMonthCheckBox.Checked ? "," + ((int)(this.WOTYMonthComboBox.SelectedIndex + 1)).ToString() : "") + ")");
            testForm.TemporalExpression = temporalExpression;
            testForm.Title = "Week of the year: " + temporalExpression.ToString();
            testForm.Description = "WOTY(n<,m>) Every n-number < m-month > of every year.  A negative value of n means reverse counting.";
            testForm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //frmTemporalExpressionTest testForm = new frmTemporalExpressionTest();
            //ITemporalExpression temporalExpression = new TemporalExpressions.TimeUnit(2, "m", new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 2, 0, 0), TimeSpan.FromSeconds(10));
            //testForm.TemporalExpression = temporalExpression;
            //testForm.ShowDialog();
        }

        private void WOTYMonthComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void DateRangeShowButton_Click(object sender, EventArgs e)
        {
            frmTemporalExpressionTest testForm = new frmTemporalExpressionTest();
            ITemporalExpression temporalExpression = TemporalExpressionFactory.CreateFromString(string.Format("DRNG({0},{1})",
                DateRangeStartDateTimePicker.Value,
                DateRangeEndDateTimePicker.Value));
            testForm.TemporalExpression = temporalExpression;
            testForm.Title = "Date range: " + temporalExpression.ToString();
            testForm.Description = "DRNG(start, end) Every day betweek two given dates.";
            testForm.ShowDialog();
        }

        private void DateUnitShowButton_Click(object sender, EventArgs e)
        {
            frmTemporalExpressionTest testForm = new frmTemporalExpressionTest();
            ITemporalExpression temporalExpression = TemporalExpressionFactory.CreateFromString(string.Format("DUNT({0},{1},{2},{3})",
                int.Parse(DateUnitUnitNumberNumericUpDown.Value.ToString()),
                DateUnits.GetKey(DateUnitUnitcomboBox.SelectedIndex).ToString(),
                DateUnitUnitStartDateTimePicker.Value,
                GetTimeSpamFromAmount(DateUnitUnitAmountComboBox.SelectedIndex)));
            testForm.TemporalExpression = temporalExpression;
            testForm.Title = "Date UNIT: " + temporalExpression.ToString();
            testForm.Description = "DUNT(n,unit, start, amount) Every n unit from star two by time amount.";
            testForm.ShowDialog();
        }


    }
}
